package com.egao.common.core.damon.utils;


import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.entity.StringEntity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//https://www.cnblogs.com/yangmengdx3/p/6322380.html
public class PhoneUtils {

    public static String BASEURL="https://app.cloopen.com:8883";

    public static void main(String[] a)throws Exception{
        PhoneUtils p = new PhoneUtils();
        p.sendMsg("13186850898","596942", new String[]{"你的船已经出发"},1);
    }

    /*
    网址
    https://www.yuntongxun.com/api/sms.html?ly=sem-baidu&qd=pc-dasou&cp=ppc&xl=null&kw=10233764&bd_vid=10625500942233933220
    0ace087fd16245d88ed41eb9638cf603
    *  @Param phoneNumber
    *  13155555,1374545454,13865464654
    * @Param content
    *
     */
    public static void sendMsg(String phoneNumber, String moudleId, String[] content, int message){
        if (message == 1){
            String datestr = DateUtil.formatDate(new Date(),"yyyyMMddhhmmss");
            JSONObject para = new JSONObject();
            para.put("to", phoneNumber);//发送的号码
            para.put("templateId", moudleId);//模块id
            para.put("appId", "8aaf07086d05d00c016d3805b9d21935");
            para.put("datas",content );
            Map<String,String> header = new HashMap<String,String>();
            String authString = "8aaf07086d05d00c016d3805b97f192f" + ":" + datestr;
            byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
            String authStringEnc = new String(authEncBytes);
            header.put("Authorization",authStringEnc );
            StringEntity httpEntity = new StringEntity(para.toString(), "UTF-8");
            httpEntity.setContentType("application/json;charset=utf-8");//发送json数据需要设置contentType

            String SigParameter = "8aaf07086d05d00c016d3805b97f192f0ace087fd16245d88ed41eb9638cf603"+datestr;
            MD5 md5 = new MD5();
            SigParameter = md5.getMD5ofStr(SigParameter);
            String surl = BASEURL +"/2013-12-26/Accounts/8aaf07086d05d00c016d3805b97f192f/SMS/TemplateSMS?sig="+SigParameter;
            try {
                String mess = HttpsUtils.post(surl,header, null, httpEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void test() throws Exception {
        String datestr = DateUtil.formatDate(new Date(),"yyyyMMddhhmmss");
        JSONObject para = new JSONObject();
        para.put("to", "13186850898");//发送的号码
        String[] content = new String[]{"测试测试测试测试测试"};//短信内容
        para.put("templateId", "474983");//模块id
        para.put("appId", "8aaf07086d05d00c016d3805b9d21935");
        para.put("datas",content );
        Map<String,String> header = new HashMap<String,String>();
        String authString = "8aaf07086d05d00c016d3805b97f192f" + ":" + datestr;
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);
        header.put("Authorization",authStringEnc );
        System.out.println(para.toString());
        StringEntity httpEntity = new StringEntity(para.toString(), "UTF-8");
        httpEntity.setContentType("application/json;charset=utf-8");//发送json数据需要设置contentType

        String SigParameter = "8aaf07086d05d00c016d3805b97f192f0ace087fd16245d88ed41eb9638cf603"+datestr;
        MD5 md5 = new MD5();
        SigParameter = md5.getMD5ofStr(SigParameter);
        String surl = BASEURL +"/2013-12-26/Accounts/8aaf07086d05d00c016d3805b97f192f/SMS/TemplateSMS?sig="+SigParameter;
        String mess = HttpsUtils.post(surl,header, null, httpEntity);
        System.out.println("surl"+surl);
        System.out.println("mess"+mess);

    }


}